package com.example.springfeature.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.springfeature.bean.IotVersion;
import com.example.springfeature.bean.SensorTest;
import com.example.springfeature.forest.MyClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ScheduledTasks {
    @Resource
    private MyClient myClient;
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 50000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));

        SensorTest sensorTest2 = SensorTest.builder()
                .pointId(15L)
                .sign("temperature-val")
                .value(RandomUtil.randomInt(0, 35))

                .build();

/**
 * 模拟iot硬件上报采集设备实时数据
 */

        List<SensorTest> list02 = new ArrayList<>();
        list02.add(sensorTest2);
        myClient.sendMsg(JSON.toJSONString(list02),
                "bios/SW715GREENJOY/sensor/upload");
    }





    @Scheduled(fixedRate = 50000)
    public void report2CurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
        SensorTest sensorTest = SensorTest.builder()
                .pointId(6L)
                .sign("temperature-val")
                .value(RandomUtil.randomInt(0, 35))

                .build();


/**
 * 模拟iot硬件上报采集设备实时数据
 */
        List<SensorTest> list01 = new ArrayList<>();
        list01.add(sensorTest);
        myClient.sendMsg(JSON.toJSONString(list01),
                "bios/SW714GREENJOY/sensor/upload");

    }







    @Scheduled(fixedRate = 50000)
    public void reportVersion(){
        //bios/{产品唯一标识}/ota/version/upload
        IotVersion iotVersion = new IotVersion();
        iotVersion.setTs(System.currentTimeMillis());
        iotVersion.setVersionCode("0.0.1");
        myClient.sendMsg(JSON.toJSONString(iotVersion), "bios/SW714GREENJOY/ota/version/upload");
    }


}
